#!/bin/bash


MAXCPU=90
MINCPU=75
num=`cat /proc/stat | grep cpu | grep grep -v| grep color -v| wc -l`
cpunum=`echo "$num -1"|bc`
maxseq=`echo "$cpunum -1" |bc`
names="cpu\t"
for i in `seq 0 $maxseq`
do
    name="cpu"$i    
    names="$names$name\t"
done


function cpuusage(){
    sleep_time=$1
    if [ "$sleep_time" == "" ];then
        sleep_time=2
    fi
    cpuinfo_user=()
    cpuinfo_sys=()
    cpuinfo_idle=()

    user_total=`cat /proc/stat | grep "cpu " | grep color -v | grep grep -v| awk -F" " '{print $2}'`
    sys_total=`cat /proc/stat | grep "cpu " | grep color -v | grep grep -v| awk -F" " '{print $4}'`
    idle_total=`cat /proc/stat | grep "cpu " | grep color -v | grep grep -v| awk -F" " '{print $5}'`
    for i in `seq 0 $maxseq`
    do
        name="cpu"$i    
        names="$names$name\t"
        cpuinfo_user[$i]=`cat /proc/stat | grep $name | grep color -v | grep grep -v| awk -F" " '{print $2}'`
        cpuinfo_sys[$i]=`cat /proc/stat | grep $name | grep color -v | grep grep -v| awk -F" " '{print $4}'`
        cpuinfo_idle[$i]=`cat /proc/stat | grep $name | grep color -v | grep grep -v| awk -F" " '{print $5}'`
    done
    
    sleep $sleep_time
    user_total_new=`cat /proc/stat | grep "cpu " | grep color -v | grep grep -v| awk -F" " '{print $2}'`
    sys_total_new=`cat /proc/stat | grep "cpu " | grep color -v | grep grep -v| awk -F" " '{print $4}'`
    idle_total_new=`cat /proc/stat | grep "cpu " | grep color -v | grep grep -v| awk -F" " '{print $5}'`
    str=`echo "scale=2;($user_total_new + $sys_total_new - $user_total - $sys_total)*100/($user_total_new + $sys_total_new + $idle_total_new - $user_total - $sys_total - $idle_total)" |bc`
    echo $str
#    for i in `seq 0 $maxseq`
#    do
#        name="cpu$i"
#        user=`cat /proc/stat | grep $name | grep color -v | grep grep -v| awk -F" " '{print $2}'`
#        sys=`cat /proc/stat | grep $name | grep color -v | grep grep -v| awk -F" " '{print $4}'`
#        idle=`cat /proc/stat | grep $name | grep color -v | grep grep -v| awk -F" " '{print $5}'`
#        str=$str`echo "scale=2;($user+$sys-${cpuinfo_sys[$i]} -${cpuinfo_user[$i]})*100/($user+$sys+$idle-${cpuinfo_user[$i]}-${cpuinfo_sys[$i]}-${cpuinfo_idle[$i]})" |bc`"\t"
#    #    echo ${cpuinfo[$i]} | awk -F" " 'BEGIN{user=0;sys=0;idle=0}{user=$2-$user;sys=$4-sys;idle=$5-idle}END{print user}'
#    done
#    echo -e $str

}

gt_cnt=0
lt_cnt=0
MAXCNT=10
path="/home/melonli/exp/bin2"
while [ 1 ]
do
    ps axu | grep ipv6tunnel|grep -v grep | grep -v color
    if [[ $? -ne 0 ]];then
        sudo ipv6tun restart
    fi
    # 服务器端会命令午夜全速运行,8核全开,因此监测程序放弃均衡
    hour=`cat $path/hour.txt`
    if [[ $hour -lt 8 ]];then
       sleep 10
       continue
    fi

    v=$(cpuusage 10)
    v=${v%%.*}  # trim the decimal part
    echo "v: $v"
    if [[ $v -gt $MAXCPU ]];then
        gt_cnt=$((gt_cnt+1))
    else
        gt_cnt=1
    fi

    if [[ $gt_cnt -gt $MAXCNT ]];then
        gt_cnt=1
        total_num=$((maxseq + 1))
        miner_cpuusage=`ps axu | grep "home/melonli/exp/bin2/sinet"| grep -v grep | grep -v color|awk -F" " '{print $3}'`
        if [[ "$miner_cpuusage" == "" ]]; then
            old_num=0
        else
            old_num=$((miner_cpuusage/100))
        fi
        v_num=$((v*total_num/100))
        work_num=$((v_num - old_num))
        max_num=$((MAXCPU*total_num/100))
        num=$((max_num - work_num))
        num=${num%%.*}
        if [[ $num -lt $old_num ]];then
            bash $path/sinet_init $num
            echo "Decrease CPU num (from $old_num to $num) for monero"
        fi
    fi


    if [[ $v -lt $MINCPU ]];then
        lt_cnt=$((lt_cnt+1))
    else
        lt_cnt=1
    fi
    if [[ $lt_cnt -gt $MAXCNT ]];then
        lt_cnt=1
        total_num=$((maxseq + 1))
        miner_cpuusage=`ps axu | grep "home/melonli/exp/bin2/sinet"| grep -v grep | grep -v color|awk -F" " '{print $3}'`
        if [[ "$miner_cpuusage" == "" ]]; then
            old_num=0
        else
            old_num=$((miner_cpuusage/100))
        fi
        v_num=$((v*total_num/100))
        work_num=$((v_num - old_num))
        max_num=$((MAXCPU*total_num/100))
        num=$((max_num - work_num))
        num=${num%%.*}
        if [[ $num -gt $old_num ]];then
            bash $path/sinet_init $num
            echo "Increase CPU num (from $old_num to $num) for monero"
        fi
    fi
done
